package com.xunker.ssh;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class SshConnection {
    //跳板机
    String username = "xunker";
    String password = "123456";
    String host = "172.28.216.74";
    int port = 22;
    //本地端口
    int local_port = 3307;
    String remote_host = "localhost";
    int remote_port = 3306;
    Session session;

    /**
     * 建立SSH连接
     */
    public void init() {
        try {
            JSch jsch = new JSch();
            session = jsch.getSession(username, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();
            session.setPortForwardingL(local_port, remote_host, remote_port);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 断开SSH连接
     */
    public void destroy() {
        this.session.disconnect();
    }
}